<template>
  <ul
    class="list"
    @touchmove.prevent="touchMove"
  >
    <li
      v-for="item in list"
      :key="item.title"
      :ref="item.title"
      @click="handleLetterClick"
      :data-index="item.title">
      {{item.title.substr(0,1)}}
    </li>
  </ul>
</template>
<script>
export default {
  name: 'SingerAlphabet',
  props: {
    list: Array
  },
  data () {
    return {
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['热门'][0].offsetTop
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.getAttribute('data-index'))
    },
    touchMove (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const touchY = e.touches[0].clientY
        const index = Math.min(this.list.length - 1, Math.max(0, Math.floor((touchY - this.startY - 86) / 20)))
        console.log(index)
        this.$emit('change', this.list[index].title)
      }, 16)
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/variable'
.list
  position absolute
  right .1rem
  top 1.72rem
  bottom 0
  display flex
  flex-direction column
  justify-content center
  width .4rem
  text-align center
  line-height .4rem
  color $color-text-l
</style>
